//-------------------------------------------------------------------------
// File: Option.cpp
//
// Descr.: - todo -
//
// Author: Stefan Leihkauf
// Date:   14.04.2014
//-------------------------------------------------------------------------
#include "cb_pch.h"
#include "Option.h"
#include "common/util/Util.h"

namespace cb { namespace detail {

using namespace cb::common::util;

//-------------------------------------------------------------------------
// options and switches are introduced by this
boost::array<StdString, 2> OptionPrefix = {{_T("--"), _T("-")}};

//-------------------------------------------------------------------------
// delimiters used to seperate an option and its value
const StdString OptionKeyValueDelimiter = _T("=");

//-------------------------------------------------------------------------
// multiple option values are delimited by this
const StdString OptionValueValueDelimiter = _T("|");

//-------------------------------------------------------------------------
Option::Option(const StdString& key) :
_key(key)
{
    // empty
}

//-------------------------------------------------------------------------
Option::Option(const StdString& key, const StdString& value) :
_key(key)
{
    this->SetValue(value);
}

//-------------------------------------------------------------------------
Option::Option(const StdString& key, const StringList& values) :
_key(key)
{
    this->SetValues(values);
}

//-------------------------------------------------------------------------
Option::~Option()
{
    // empty
}

//-------------------------------------------------------------------------
void Option::SetValue(const StdString& value)
{
    _values.clear();

    if (false == value.empty()) {
        _values.push_back(value);
    }
}

//-------------------------------------------------------------------------
void Option::SetValues(const StringList& values)
{
    for (unsigned i = 0; i < values.size(); i++) {
        if (false == values[i].empty()) {
            if (false == ValueExists(values[i]))
                _values.push_back(values[i]);
        }
    }
}

//-------------------------------------------------------------------------
StdString Option::Key() const
{
    return _key;
}

//-------------------------------------------------------------------------
const StringList& Option::Values() const
{    
    return _values;
}

//-------------------------------------------------------------------------
unsigned Option::NumValues() const
{
    return _values.size();
}

//-------------------------------------------------------------------------
bool Option::HaveValue(const StdString& name) const
{
    if (false != name.empty())
        return ValueExists(name);

    return false;
}

//-------------------------------------------------------------------------
bool Option::IsEmpty() const
{
    return ((_key.empty()) && (_values.empty()));
}

//-------------------------------------------------------------------------
bool Option::operator == (const Option& other) const
{
    if ((_key != other._key) || (_values.size() != other._values.size()))
        return false;

    for (unsigned i = 0; i < _values.size(); i++) {
        if (_values[i] != other._values[i])
            return false;
    }

    return true;
}

//-------------------------------------------------------------------------
/*static*/ Option Option::Empty()
{
    return Option(StdString());
}

//-------------------------------------------------------------------------
/*static*/ Option Option::FromString(const StdString& line)
{
    //StdString strTemp(line);

    //StringTrimLeftRight(strTemp);

    //// check if prefixed by one of the option/switch prefixes
    //for(unsigned i = 0; i < OptionPrefix.size(); i++) {
    //    if(strTemp.find(OptionPrefix[i]) == 0) {
    //        
    //        // remove option/switch prefix
    //        strTemp = strTemp.substr(OptionPrefix[i].length());

    //        // split by option-value delimiter
    //        StringList sl = StringSplit(strTemp, OptionKeyValueDelimiter);

    //        if(
    //    }
    //}

    return Option(_T(""));
}

//-------------------------------------------------------------------------
bool Option::ValueExists(const StdString& value) const
{
    for (unsigned i = 0; i < _values.size(); i++) {
        if (_values[i] == value)
            return true;
    }
    return false;
}

}} // namespace